(function ($) {
    var cityCN = [0, 0, 0], codeurl = null;

    $.extend({
        initcity: function (url, cityCode) {
            cityCN = getCityArray(cityCode+"") || cityCN;
            codeurl = url || null;
            // console.log(getCityArray(cityCode) )
            // console.log(codeurl)
            initAddress();
        },
        getCityValue: function () {
            return cityCN[2] != 0 ? cityCN[2] : cityCN[1] != 0 ? cityCN[1] : cityCN[0] == 0 ? 0 : cityCN[0]
        }

    })

    //城市id 分成数组
    function getCityArray(cityCode) {
        var cityCN = new Array();
        if (cityCode == null || cityCode == undefined)
            return null;
        switch (cityCode.length) {
            case 2: {
                cityCN[0] = cityCode.substring(0, 2);
                cityCN[1] = 0;
                cityCN[2] = 0;
                break;
            }
            case 4: {
                cityCN[0] = cityCode.substring(0, 2);
                cityCN[1] = cityCode.substring(0, 4);
                cityCN[2] = 0;
                break;
            }
            case 6: {
                cityCN[0] = cityCode.substring(0, 2);
                cityCN[1] = cityCode.substring(0, 4);
                cityCN[2] = cityCode.substring(0, 6);
                break;
            }
        }
        return cityCN;
    }

//获取省份
    function initAddress() {
        // ajax请求所有省份
        $("#province").empty().append('<option value=0>-- 选择省 --</option>');
        $.post(codeurl, {pid: 0}, function (data) {
            $.each(data.rows, function (i, d) {
                $("#province").append("<option " + (d.id == cityCN[0] ? 'selected' : '') + " value='" + d.id + "'>" + d.name + "</option>");
            });
            // 根据第一个省份code获取对应城市列表
            getCity();
        }, 'json');
    }

//监听省份点击
    $("select[name=districts]").on('change', function () {
        //点击的省，市，县 code值
        var v = $(this).val(),
            //点击的 select 序号
            is = $(this).attr("data-i");
        if (is == 0)
            cityCN[1] = 0, cityCN[2] = 0, initAddress();
        else if (is == 1)
            cityCN[2] = 0, getCity()
        else if (is == 2)
            getDistrict()

        cityCN[is] = v;
        console.log(is, v, cityCN)

    })

//获取对应城市列表（里面包括获取第一个城市的区县列表）
    function getCity() {
        // console.log("getCity", provCode, code)
        var firstCityCode;
        // ajax请求所有市级单位
        $.post(codeurl, {pid: cityCN[0]}, function (data) {
            // 先清空城市下拉框
            $("#city").empty().append('<option value=0>---- 选择市 ----</option>');
            $.each(data.rows, function (i, d) {
                $("#city").append("<option " + (d.id == cityCN[1] ? 'selected' : '') + " value='" + d.id + "'>" + d.name + "</option>");
            });
            // 根据第一个城市code获取对应区县列表
            getDistrict();
        }, 'json');
    }

    function getDistrict() {
        // console.log("cityCode", cityCode, code)
        // ajax请求所有区县单位
        $.post(codeurl, {pid: cityCN[1]}, function (data) {
            // 先清空城市下拉框
            $("#districtsId").empty().append('<option value=0>---- 选择区 ----</option>');
            $.each(data.rows, function (i, d) {
                $("#districtsId").append("<option " + (d.id == cityCN[2] ? 'selected' : '') + " value='" + d.id + "'>" + d.name + "</option>");
            });
        }, 'json');
    }


})(jQuery);
